const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

//判断是否为空
const isEmpty = value => {
  if (value == "") {
    return true;
  } else {
    return false;
  }
}

//四舍五入保留两位小数
const keepPointTow = num => {
  var result = parseFloat(num);
  if (isNaN(result)) {
    wx.showToast({
      title: '参数传递错误',
      icon: 'none',
      duration: 2000
    })
    return false;
  }
  result = Math.round(num * 100) / 100;
  return result;
}

//判断一个数是否正常
function judgeNum(val) {
  val = val.toString()
  let valArr = val.match(/^-?\d+\.?\d*$/g)
  // 如果只有数字、小数点、-号
  if (valArr){
    return true
  }else{
    return false
  }
}

//获取两个数的最大公约数
function getMaxPublicNum(k, b) {
  k = Math.abs(k)
  b = Math.abs(b)
  var smallNum; //两者中较小的数
  var maxPublicNum = 1; //最大公约数
  smallNum = Math.min(k, b)
  console.log('较小的数是', smallNum)
  // 循环找到分子分母的最大公约数
  for (var i = 0; i < smallNum; i++) {
    if (k % i === 0 && b % i === 0) {
      console.log(i)
      maxPublicNum = i;
    }
  }
  return maxPublicNum
}

function getShang(x, y) {
  var json = {}
  y = y.toString()
  if(y === ''|| y === '0'){
    wx.showToast({
      title: '数值错误',
      icon: 'none'
    })
    return
  }
  let shang = parseFloat(x) / parseFloat(y)
  shang < 0 ? json.isRetMinus = true : json.isRetMinus = false
  x = Math.abs(x)
  y = Math.abs(y)
  let ret = x / y
  ret = ret.toString()
  ret = ret.match(/(?<=\.)\d+/)
  // 如果商包含小数，并且小数的位数大于5则代表除不尽
  if (ret && ret[0].length > 5) {
    //除不尽
    console.log('除不尽')
    x = x.toString()
    y = y.toString()
    if (x.indexOf('.') > 0 || y.indexOf('.') > 0) {
      console.log('被除数或除数包含小数')
      let afterPointNumXarr = x.match(/(?<=\.)\d+/)
      let afterPointNumYarr = y.match(/(?<=\.)\d+/)
      let afterPointNumX, afterPointNumY, raiseNum
      afterPointNumXarr ? afterPointNumX = afterPointNumXarr[0] : afterPointNumX = '0'
      afterPointNumYarr ? afterPointNumY = afterPointNumYarr[0] : afterPointNumY = '0'
      console.log('小数点后的数是', afterPointNumX)
      console.log(afterPointNumX.length)
      console.log(afterPointNumY.length)
      afterPointNumX.length > afterPointNumY.length ? raiseNum = afterPointNumX.length : raiseNum = afterPointNumY.length
      console.log('扩大的倍数是', raiseNum)
      x = Math.pow(10, parseInt(raiseNum)) * x
      y = Math.pow(10, parseInt(raiseNum)) * y
      console.log('变换之后的数是', y)
      // let maxNum = getMaxPublicNum(x, y)
      // console.log('最大公因数是', maxNum)
      // x = x / maxNum
      // y = y / maxNum
      // console.log('最终的x是', x)
      // console.log('最终的y是', y)
    } else {
      console.log('被除数和除数都不是小数')
    }
    let maxNum = getMaxPublicNum(x, y)
    console.log('最大公因数是', maxNum)
    x = x / maxNum
    y = y / maxNum
    console.log('最终的x是', x)
    console.log('最终的y是', y)
    json.fenzi = x
    json.fenmu = y
    return json
  } else {
    // 可以除尽
    console.log('可以除尽')
    json.fenzi = x / y
    json.fenmu = 0
    return json
  }
}

//获取平方根
function getSquare(num) {
  let square = Math.sqrt(num)
  square = square.toString()
  if (square.length > 5) {
    console.log('不能整数平方根')
    let integerNum = 1,
      squareNum;
    let numArr = [4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]
    numArr.forEach(function(val) {
      // console.log(num)
      if (num % val === 0) {
        integerNum = Math.sqrt(val)
        squareNum = num / val
        return
      }
    })
    if (integerNum !== 1) {
      console.log('能根除，整数部分是', integerNum)
      let ret = {}
      ret.integerNum = integerNum
      ret.squareNum = squareNum
      return ret
    } else {
      console.log('不能根除结果是')
      // return '√' + num
      let ret = {}
      ret.integerNum = 0
      ret.squareNum = num
      return ret
    }
  } else {
    console.log('得到整数平方根是', square)
    let ret = {}
    ret.integerNum = square
    ret.squareNum = 0
    return ret
  }
}

module.exports = {
  formatTime: formatTime,
  isEmpty: isEmpty,
  keepPointTow: keepPointTow,
  judgeNum: judgeNum,
  getMaxPublicNum: getMaxPublicNum,
  getShang: getShang,
  getSquare: getSquare
}